$(shell [[ ! -d docs ]] && mkdir docs )
$(shell [[ ! -d pb ]] && mkdir pb )

PROTO_DIR = ./proto
PROTO_BUF_DIR=$(shell go list -f '{{ .Dir }}' -m github.com/golang/protobuf)
GATE_WAY_DIR=$(shell go list -f '{{ .Dir }}' -m github.com/grpc-ecosystem/grpc-gateway)

PROTO_INCLUDE_DIR = -I$(PROTO_BUF_DIR) \
	-I$(PROTO_DIR) \
	-I$(GATE_WAY_DIR)/third_party/googleapis \

install-swagger:
	go get github.com/go-swagger/go-swagger/cmd/swagger
	go get github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc

install-grpc-gateway:
	# install protoc
	go get github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
	go get github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
	go get github.com/golang/protobuf/protoc-gen-go

install-protoc:
	brew install protobuf

install-tools: install-swagger install-grpc-gateway install-protoc

build-protos:
	protoc $(PROTO_INCLUDE_DIR) --go_out=plugins=grpc:pb $(wildcard $(PROTO_DIR)/*.proto)


build-proto-docs:
	protoc $(PROTO_INCLUDE_DIR) --doc_out=docs --doc_opt=markdown,all-pb.md $(PROTO_DIR)/*.proto
	protoc $(PROTO_INCLUDE_DIR) --swagger_out=logtostderr=true:docs $(PROTO_DIR)/*.proto
	swagger generate spec -b ./api -o docs/plugin.swagger.json
	#swagger-markdown -i docs/api.swagger.json -o docs/README.md
	#swagger-markdown -i docs/plugin.swagger.json -o docs/README_plugin.md

# 构建 *.pb.gw.go grpc-gateway 代理
build-proxy:
	protoc $(PROTO_INCLUDE_DIR) --grpc-gateway_out=logtostderr=true:pb api.proto

# mock 数据 方便go test
install-mock-gen:
	@go install -v ./vendor/github.com/golang/mock/gomock
	@go install -v ./vendor/github.com/golang/mock/mockgen

mock:
	@mockgen --source ./pb/api.pb.go --package pb --destination ./pb/api.pb_mock.go
	@mockgen --source ./pb/user.pb.go --package pb --destination ./pb/user.pb_mock.go

test:
	@go test ./... -v -coverprofile .testCoverage.txt

binary:
	@echo building binary
	@mkdir -p bin
	@go build -o bin/server cmd/main.go

build: build-protos build-proxy build-proto-docs binary



.PHONY: install-swagger install-protoc install-grpc-gateway install-tools build-protos build-proxy binary build
